package com.ht.medicaltreatment.activity.haocai2

import android.content.Intent
import android.os.Environment
import android.view.View
import androidx.activity.viewModels
import androidx.recyclerview.widget.LinearLayoutManager
import com.blankj.utilcode.util.FileUtils
import com.blankj.utilcode.util.GsonUtils
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.listener.OnItemChildClickListener
import com.chad.library.adapter.base.listener.OnItemClickListener
import com.gym.baselib.base.BaseActivity
import com.ht.medicaltreatment.R
import com.ht.medicaltreatment.adapter.FileListAdapter
import com.ht.medicaltreatment.bean.ConsumableRecordData
import com.ht.medicaltreatment.bean.FilePathBean
import com.ht.medicaltreatment.view.InputCountDialog
import com.ht.medicaltreatment.viewmodel.PrepareConsumableViewModel
import kotlinx.android.synthetic.main.activity_prepare_consumable_layout.fileRecyclerView
import kotlinx.android.synthetic.main.activity_prepare_consumable_layout.recyclerView
import kotlinx.android.synthetic.main.activity_prepare_consumable_layout.tvAdd
import kotlinx.android.synthetic.main.activity_prepare_consumable_layout.viewBg
import java.io.File

/**
 * 耗材审批 -> 准备耗材
 */
class PrepareConsumableActivity:BaseActivity(), View.OnClickListener, OnItemClickListener,
    OnItemChildClickListener {
    override fun getLayoutId() = R.layout.activity_prepare_consumable_layout

    private val file = File(Environment.getExternalStorageDirectory(), "惠通医疗")
    //private val file = File(Environment.getExternalStorageDirectory(), "Pictures")
    private lateinit var itemAdapter:FileListAdapter
    private lateinit var adapter:PrepareConsumableAdapter
    private val model by viewModels<PrepareConsumableViewModel>()
    private var projectId = 0
    private var id = 0
    override fun init() {
        setTitle("耗材准备记录")
        id = intent.getIntExtra("id",0)
        recyclerView.layoutManager = LinearLayoutManager(this)
        adapter = PrepareConsumableAdapter();
        recyclerView.adapter = adapter
        adapter.setOnItemChildClickListener(this)

        viewBg.setOnClickListener(this)
        tvAdd.setOnClickListener(this)
        FileUtils.createOrExistsDir(file)
        fileRecyclerView.layoutManager = LinearLayoutManager(this)
        itemAdapter = FileListAdapter()
        fileRecyclerView.adapter = itemAdapter
        itemAdapter.setOnItemClickListener(this)

    }

    override fun onResume() {
        super.onResume()
        projectId = intent.getIntExtra("projectId",0)
        initData(projectId)
    }

    fun open() {
        fileRecyclerView.setVisibility(View.VISIBLE)
        viewBg.setVisibility(View.VISIBLE)
    }

    fun hide() {
        viewBg.setVisibility(View.GONE)
        fileRecyclerView.setVisibility(View.GONE)
    }

    private fun initData(projectId:Int) {
        showProgress()
        model.requestList(projectId){
            dismissProgress()
            adapter.setNewInstance(it)
        }
    }

    override fun onClick(v: View?) {
        when(v){
            tvAdd -> {
                //手动添加页面
                //startActivity(CreateConsumableMoreActivity::class.java)
                val list = FileUtils.listFilesInDir(file)
                val listFile = arrayListOf<FilePathBean>()
                list.forEach {
                    listFile += FilePathBean(it.name,it.absolutePath)
                }
                itemAdapter.setNewInstance(listFile)
                open()
            }

            viewBg -> {
                hide()
            }
        }
    }

    override fun onItemClick(adapter: BaseQuickAdapter<*, *>, view: View, position: Int) {
        if(adapter is FileListAdapter){
            hide()
            val item = adapter.getItem(position)
            InputCountDialog(this,-1).showDialog {
                showProgress(true)
                model.postFile(item.path,projectId,id,it.toInt(),{
                    dismissProgress()
                    val intent = Intent(this,CreateConsumableActivity::class.java)
                    intent.putExtra("json",GsonUtils.toJson(it))
                    startActivity(intent)
             })
            }
        }
    }

    override fun onItemChildClick(adapter: BaseQuickAdapter<*, *>, view: View, position: Int) {
        val item = adapter.getItem(position) as ConsumableRecordData
        val intent = Intent(this,CreateConsumableActivity::class.java)
        intent.putExtra("id",item.id)
        startActivity(intent)
    }
}